package com.nanyang.tms.controller.company;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.nanyang.common.core.annotation.BindParam;
import com.nanyang.common.core.dict.CommonEnum;
import com.nanyang.common.core.domain.ApiResult;
import com.nanyang.common.core.exception.ServiceException;
import com.nanyang.common.core.page.PageBean;
import com.nanyang.tms.api.dto.request.company.*;
import com.nanyang.tms.api.dto.request.companypickaddress.CompanyPickAddressAddRequest;
import com.nanyang.tms.api.dto.response.company.CompanyDetailResponse;
import com.nanyang.tms.api.dto.response.company.CompanyPageKeyResponse;
import com.nanyang.tms.api.dto.response.company.CompanyPageResponse;
import com.nanyang.tms.api.dto.response.company.CompanyResponse;
import com.nanyang.tms.api.service.company.CompanyRpcService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/company")
public class CompanyController {

    @Reference(version = "1.0.0", timeout = 10000, check = false)
    private CompanyRpcService companyRpcService;

    /**
     * <p>listCompany</p>
     * <p>Description:查询列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public ApiResult<PageBean<CompanyPageResponse>> listCompany(@RequestBody CompanyQueryRequest param) {
        ApiResult<PageBean<CompanyPageResponse>> apiResult = companyRpcService.listCompany(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listCompany</p>
     * <p>Description:模糊查询列表</p>
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/listByKey", method = RequestMethod.POST)
    public ApiResult<PageBean<CompanyPageKeyResponse>> listCompanyByKey(@RequestBody CompanyQueryRequest param) {
        ApiResult<PageBean<CompanyPageKeyResponse>> apiResult = companyRpcService.listCompanyByKey(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listCompany</p>
     * <p>Description:查询根据编码</p>
     *
     * @param companyCode
     * @return
     */
    @BindParam
    @RequestMapping(value = "/detail/{companyCode}", method = RequestMethod.GET)
    public ApiResult<CompanyDetailResponse> detail(@PathVariable("companyCode")String companyCode) {
        ApiResult<CompanyDetailResponse> apiResult = companyRpcService.detail(companyCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * <p>listCompany</p>
     * <p>Description:查询根据编码</p>
     *
     * @param companyCode
     * @return
     */
    @BindParam
    @RequestMapping(value = "/selectByCode", method = RequestMethod.GET)
    public ApiResult<CompanyResponse> selectByCode(@Value("") @BindParam  String companyCode) {
        ApiResult<CompanyResponse> apiResult = companyRpcService.selectByCode(companyCode);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 禁用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/disable", method = RequestMethod.POST)
    public ApiResult<Boolean> disableState(@BindParam @RequestBody @Valid CompanyUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.disableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 启用
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/enable", method = RequestMethod.POST)
    public ApiResult<Boolean> enableState(@BindParam @RequestBody @Valid CompanyUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.enableState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 通过
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/pass", method = RequestMethod.POST)
    public ApiResult<Boolean> passState(@BindParam @RequestBody @Valid CompanyUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.passState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 拒绝
     * @param param
     * @return
     */
    @BindParam
    @RequestMapping(value = "/reject", method = RequestMethod.POST)
    public ApiResult<Boolean> rejectState(@BindParam @RequestBody @Valid CompanyUpdateStateRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.rejectState(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    /**
     * 添加-前台
     * @param param
     * @return
     */

    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> saveProduct(@BindParam @RequestBody @Valid CompanyAddRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.saveCompany(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 修改-前台
     * @param param
     * @return
     */

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> updateCompany(@BindParam @RequestBody @Valid CompanyUpdateRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.updateCompany(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }
    /**
     * 设置会员等级
     * @param param
     * @return
     */

    @RequestMapping(value = "/updateLevel", method = RequestMethod.POST)
    @BindParam
    public ApiResult<Boolean> updateCompanyLevel(@BindParam @RequestBody @Valid CompanyUpdateLevelRequest param) {
        ApiResult<Boolean> apiResult = companyRpcService.updateCompanyLevel(param);
        if (apiResult.getCode() == CommonEnum.SYSTEM_EXCEPTION.getCode()) {
            throw new ServiceException(CommonEnum.SYSTEM_EXCEPTION.getCode(), apiResult.getMessage());
        }
        return apiResult;
    }

    public static void main(String[] args) {
        String s = JSONObject.toJSONString(new CompanyPickAddressAddRequest(), SerializerFeature.WriteMapNullValue);
        System.out.println(s);
    }
}
